package work.linruchang.util;

import cn.hutool.core.util.ArrayUtil;

import java.util.Arrays;

/**
 * 增强的数组处理工具
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2021/07/28
 * @since 1.8
 **/
public class EnhanceArrayUtil extends ArrayUtil {

    /**
     * 将数组转成字符串
     * @param object 必须确保是数组，否则会抛出异常
     * @return
     */
    public static String toString(Object object) {
        if(object == null) {
            return null;
        }else {
            EnhanceAssert.isTrue(isArray(object),"请确保实参是数组类型");
            Class<?> targetClass = object.getClass();
            if(targetClass == int[].class) {
                return Arrays.toString((int[]) object);
            }else if(targetClass == byte[].class) {
                return Arrays.toString((byte[]) object);
            }else if(targetClass == short[].class) {
                return Arrays.toString((short[]) object);
            }else if(targetClass == long[].class) {
                return Arrays.toString((long[]) object);
            }else if(targetClass == char[].class) {
                return Arrays.toString((char[]) object);
            }else if(targetClass == boolean[].class) {
                return Arrays.toString((boolean[]) object);
            }else if(targetClass == float[].class) {
                return Arrays.toString((float[]) object);
            }else if(targetClass == double[].class) {
                return Arrays.toString((double[]) object);
            }else {
                return Arrays.toString((Object[]) object);
            }
        }
    }

    /**
     * 获取数组第一个元素
     * @param array
     * @return
     */
    public static <T> T getFirst(T[] array) {
        return isNotEmpty(array) ? array[0] : null;
    }

    public static void main(String[] args) {

        String join = ArrayUtil.join(new String[]{"332", "dsfsd"}, ", ");

        System.out.println(join);


    }

}
